package com.hnichr.crm.biz.report.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.hnichr.crm.common.customer.service.ICrmCustomerService;
import com.hnichr.crm.common.receipt.entity.CrmReceipt;
import com.hnichr.crm.common.receipt.service.ICrmReceiptService;
import com.hnichr.crm.common.report.repository.RptRepository;
import com.hnichr.crm.common.report.vo.CrmAnalysis;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 报表Controller
 *
 * @author Wp
 * @date 2022-12-24
 */
@Slf4j
@Tag(name = "报表Controller", description = "报表")
@RestController()
@RequestMapping("/report")
public class CrmReportController {

    @Autowired
    private ICrmReceiptService crmReceiptService;
    @Autowired
    private RptRepository rptRepository;
    @Autowired
    private ICrmCustomerService customerService;

    /**
     * 本月已收款开票客户明细
     */
    @GetMapping("/customerreceipts")
    @Operation(summary = "收款单", description = "分页查询", method = "GET")
    public Result<Page<CrmReceipt>> customerreceipts(Criteria criteria,
                                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        String loginUsername = SecurityUtils.getLoginUsername();
        criteria.and()
                .addCriterion("payTime", Operator.GE, DateUtil.beginOfMonth(new Date()))
                .addCriterion("payTime", Operator.LE, DateUtil.endOfMonth(new Date()))
                .end();
        Page<CrmReceipt> page = crmReceiptService.page(criteria, pageAble);
        return Result.OK(page);
    }

    /**
     * 累计收款开票前十大客户明细
     */
    @GetMapping("/receiptstop10")
    @Operation(summary = "累计收款开票前十大客户", description = "分页查询", method = "GET")
    public Result<Page<Map<String, Object>>> receiptstop10(Criteria criteria,
                                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                           @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        Page<Map<String, Object>> page = rptRepository.findTop10Customer(pageAble);
        return Result.OK(page);
    }

    /**
     * TOP10客户
     * 根据客户合同签订金额排列出与我司合作前十大客户
     */
    @GetMapping("/contacttop10")
    @Operation(summary = "累计收款开票前十大客户", description = "分页查询", method = "GET")
    public Result<Page<Map<String, Object>>> contracttop10(Criteria criteria,
                                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                           @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        Page<Map<String, Object>> page = rptRepository.findTop10(pageAble);
        return Result.OK(page);
    }
    /**
     * 首页
     *
     */
    @GetMapping("/analysis")
    @Operation(summary = "累计收款开票前十大客户", description = "分页查询", method = "GET")
    public Result<CrmAnalysis> analysis() throws Exception {
        CrmAnalysis analysis = customerService.getAnalysis();
        return Result.OK(analysis);
    }
    /**
     * 首页-本月新增项目数量
     *
     */
    @GetMapping("/project/increase")
    @Operation(summary = "首页-本月新增项目数量", description = "分页查询", method = "GET")
    public Result<List<Map<String, Object>>> increaseProject() throws Exception {
        Date now = new Date();
        DateTime beginTime = DateUtil.beginOfMonth(now);
        DateTime endTime = DateUtil.offsetMonth(beginTime, 1);
        List<Map<String, Object>> result = rptRepository.getIncreaseProject(beginTime, endTime);
        return Result.OK(result);
    }

}
